import {createRouter, createWebHistory} from 'vue-router'
import editPage from "@/pages/editPage";
import order_outPage from "@/pages/order_outPage";
import LoginPage from "@/pages/loginPage.vue";
import MainPage from "@/pages/mainPage.vue";
import PersonCenter from "@/pages/userInfoPage.vue"
import CookerDetailPage from "@/pages/CookInfo.vue"
import OrderInPage from "@/pages/order_inPage.vue"
import cookChat from "@/pages/cookChat.vue"

const routes = [
    {
        path: '/',
        redirect: '/loginPage', // 默认路由的重定向路径
    },
    {
        name:'登录界面',
        path: '/loginPage',
        components: {
            mainRouterView: LoginPage
        },
        meta: {
            requiresAuth: true
        }
    },
    {
        name:'主界面',
        path: '/mainPage',
        components: {
            mainRouterView: MainPage
        },
        meta: {
            requiresAuth: true, // 需要登录才能访问
        },

    },
    {
        name:'厨师详细界面',
        path: '/cookerDetail/:id',
        components: {
            mainRouterView: CookerDetailPage
        },    meta: {
            requiresAuth: true, // 需要登录才能访问
        },

        },
    {
        name:'厨师对话页面',
        path: '/cookChat',
        components: {
            mainRouterView: cookChat
        },    meta: {
            requiresAuth: true, // 需要登录才能访问
        },
    },
    {
        name:'订单详细界面',
        path: '/orderDetail/:id',
        components: {
            mainRouterView: OrderInPage
        },    meta: {
            requiresAuth: true, // 需要登录才能访问
        },

        },
    {
        name:'个人中心',
        path: '/personCenter',
        redirect: '/personCenter/order_out', // 重定向到订单界面
        components: {
            mainRouterView: PersonCenter
        },    meta: {
            requiresAuth: true, // 需要登录才能访问
        },
        children: [
            {
                path: 'edit',
                component: editPage ,meta: {
                    requiresAuth: true, // 需要登录才能访问
                },
            },
            {
                path: 'order_out',
                component: order_outPage, meta: {
                    requiresAuth: true, // 需要登录才能访问
                },
            }
        ]
    },
]


// export default createRouter({
//     history: createWebHistory(),
//     routes:routes
// })

const router = createRouter({
    history: createWebHistory(),
    routes
});
export default router
//
// // 添加全局路由守卫
// router.beforeEach((to, from, next) => {
//     // 检查路由是否需要登录才能访问
//     const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
//     if (requiresAuth && !isLoggedIn()) {
//         // 如果需要登录但当前未登录，则跳转到登录页面
//         console.log(to.meta.requiresAuth)
//         next('/loginPage');
//     } else {
//         next(); // 继续正常跳转
//     }
// });
// const {proxy} =getCurrentInstance()
// // 模拟检查是否已登录的函数
// function isLoggedIn() {
//     // 实际情况下根据你的认证逻辑进行判断
//     // 返回true表示已登录，返回false表示未登录
//     if (proxy.$isLogin.value!==true)
//     {
//         return false; // 这里假设已登录
//     }
//     return true
// }
